#Boa:Dialog:BuscarDescrip

import wx
import wx.grid
import consultasBD

def create(parent):
    return BuscarDescrip(parent)

[wxID_BUSCARDESCRIP, wxID_BUSCARDESCRIPGRID1, wxID_BUSCARDESCRIPINBUSCAR, 
 wxID_BUSCARDESCRIPINPUT_BUSCAR, wxID_BUSCARDESCRIPSTATICTEXT1, 
 wxID_BUSCARDESCRIPSTATICTEXT2, wxID_BUSCARDESCRIPTEXTCTRL1, 
] = [wx.NewId() for _init_ctrls in range(7)]

class BuscarDescrip(wx.Dialog):
    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Dialog.__init__(self, id=wxID_BUSCARDESCRIP, name='BuscarDescrip',
              parent=prnt, pos=wx.Point(413, 145), size=wx.Size(704, 411),
              style=wx.DEFAULT_DIALOG_STYLE,
              title=u'Buscar en la descripci\xf3n')
        self.SetClientSize(wx.Size(696, 384))

        self.staticText1 = wx.StaticText(id=wxID_BUSCARDESCRIPSTATICTEXT1,
              label='Buscar productos cuya descripcion o rubro contenga',
              name='staticText1', parent=self, pos=wx.Point(8, 8),
              size=wx.Size(251, 13), style=0)

        self.input_buscar = wx.TextCtrl(id=wxID_BUSCARDESCRIPINPUT_BUSCAR,
              name=u'input_buscar', parent=self, pos=wx.Point(8, 24),
              size=wx.Size(328, 21), style=0, value='')

        self.inbuscar = wx.Button(id=wxID_BUSCARDESCRIPINBUSCAR, label='Buscar',
              name='inbuscar', parent=self, pos=wx.Point(608, 24),
              size=wx.Size(75, 23), style=0)
        self.inbuscar.Bind(wx.EVT_BUTTON, self.OnInbuscarButton,
              id=wxID_BUSCARDESCRIPINBUSCAR)

        self.grid1 = wx.grid.Grid(id=wxID_BUSCARDESCRIPGRID1, name='grid1',
              parent=self, pos=wx.Point(8, 56), size=wx.Size(680, 320),
              style=0)
        self.grid1.Bind(wx.grid.EVT_GRID_CELL_LEFT_DCLICK,
              self.OnGrid1GridCellLeftDclick)

        self.staticText2 = wx.StaticText(id=wxID_BUSCARDESCRIPSTATICTEXT2,
              label='para el proveedor', name='staticText2', parent=self,
              pos=wx.Point(344, 8), size=wx.Size(86, 13), style=0)

        self.textCtrl1 = wx.TextCtrl(id=wxID_BUSCARDESCRIPTEXTCTRL1,
              name='textCtrl1', parent=self, pos=wx.Point(344, 24),
              size=wx.Size(100, 21), style=0, value='')
        self.textCtrl1.SetEditable(False)

    def __init__(self, parent, proveedor):
        self._init_ctrls(parent)
        self.codsussan = ""
        self.codartprov = ""
        self.proveedor = proveedor
        self.textCtrl1.SetLabel(str(proveedor))
        self.grillafactura()

    def autosizegrilla(self):
        for i in xrange(self.grid1.GetNumberCols()):
            self.grid1.AutoSizeColumn(i,True)

    def grillafactura(self):
        columnas =['Cod.Articulo\nProveedor','Codigo\nSussan','Descripcion', 'Estacion','Anio','$ Costo','$ Venta','Grupo']

        self.grid1.CreateGrid(1,len(columnas))

        for i, col in enumerate(columnas): 
            self.grid1.SetColLabelValue(i, col)
        self.autosizegrilla()
        self.grid1.SetSelectionMode(wx.grid.Grid.wxGridSelectRows)
        self.grid1.EnableEditing(False)

    def _limpiar_grilla(self):
        self.grid1.ClearGrid()
        for fil in xrange(self.grid1.GetNumberRows()):
            self.grid1.DeleteRows()
        self.grid1.AppendRows()

    def busca(self, codigo):
        self.inbuscar.SetLabel("Procesando")
        self.inbuscar.Enable(False)
        self._limpiar_grilla()
        r1 = "SELECT * FROM productos p "
        r2 = "JOIN producto_codartprov cb ON (p.codsussan = cb.codsussan) "
        r3 = "JOIN grupos g ON (p.grupo = g.idgrupo) "
        r4 = "WHERE (p.descri LIKE '%%%s%%' OR g.nombregrupo LIKE '%%%s%%') " % (str(codigo), str(codigo))
        r5 = "       AND p.proveedor = '%s'" % (str(self.proveedor))
        sql = r1+r2+r3+r4+r5
        resultado = consultasBD.ordenCrudaDict(sql)
        for i, r in enumerate(resultado):
            self.grid1.SetCellValue(i, 0, str(r['codartprov']))
            self.grid1.SetCellAlignment(i,0,wx.ALIGN_CENTRE,wx.ALIGN_CENTRE)
            self.grid1.SetCellValue(i, 1, str(r['codsussan']))
            self.grid1.SetCellAlignment(i,1,wx.ALIGN_CENTRE,wx.ALIGN_CENTRE)
            self.grid1.SetCellValue(i, 2, str(r['descri']))
            self.grid1.SetCellValue(i, 3, str(r['estacion']))
            self.grid1.SetCellValue(i, 4, str(r['anio']))
            self.grid1.SetCellValue(i, 5, str(r['preciocompra']))
            self.grid1.SetCellAlignment(i,5,wx.ALIGN_RIGHT,wx.ALIGN_CENTRE)
            self.grid1.SetCellValue(i, 6, str(r['preciolt1']))
            self.grid1.SetCellAlignment(i,6,wx.ALIGN_RIGHT,wx.ALIGN_CENTRE)
            self.grid1.SetCellValue(i, 7, str(r['grupo'])+" / "+str(r['nombregrupo']))
            self.grid1.AppendRows()
        self.autosizegrilla()
        self.inbuscar.SetLabel("Buscar")
        self.inbuscar.Enable(True)
        self.input_buscar.SetFocus()

    def Muestraid(self):
        return (self.codsussan, self.codartprov)

    def OnInbuscarButton(self, event):
        busqueda = self.input_buscar.Value
        self.busca(busqueda)

    def OnGrid1GridCellLeftDclick(self, event):      
        fila = self.grid1.GetGridCursorRow()
        id = self.grid1.GetCellValue(fila, 0)
        codsussan = self.grid1.GetCellValue(fila, 1)

        self.codsussan = codsussan
        self.codartprov = id
        self.Close()        
